package com.cloudream.toucheventdemo.dispatchiner;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ListView;

public class MyListView extends ListView {

    private static final String TAG = "MyListView";

    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    private IParentState mParentState;



    public IParentState getmParentState() {
        return mParentState;
    }

    public void setmParentState(IParentState mParentState) {
        this.mParentState = mParentState;
    }


    float mLastX;
    float mLastY;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {


        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mLastY = ev.getY();
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                float direction = ev.getY() - mLastY;

                if(mParentState.canScrollUp() && direction < 0){
                    //getParent().requestDisallowInterceptTouchEvent(false);

                    Log.d(TAG,"ACTION_MOVE- STATE1");

                }else if(!mParentState.canScrollUp() && direction < 0){
                    getParent().requestDisallowInterceptTouchEvent(true);
                    Log.d(TAG,"ACTION_MOVE- STATE2");
                }else if(!canScrollVertically(-1) && direction > 0 &&
                mParentState.canScrollDown()){
                    getParent().requestDisallowInterceptTouchEvent(false);
                    Log.d(TAG,"ACTION_MOVE- STATE3");
                }else if(canScrollVertically(-1) && direction > 0){
                    getParent().requestDisallowInterceptTouchEvent(true);
                    Log.d(TAG,"ACTION_MOVE- STATE4");
                }else{
                    Log.d(TAG,"ACTION_MOVE- STATE5");
                }
                break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG,"ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d(TAG,"ACTION_CANCEL");
                break;
        }


        return super.dispatchTouchEvent(ev);
    }




    public interface IParentState{
        boolean canScrollDown();
        boolean canScrollUp();
    }
}
